#!/bin/sh


SDK_DIR=~/f1c200s_linux_sdk
SDK_DIR_BUILDROOT=${SDK_DIR}/buildroot-overlay

IMAGE_DIR=${PWD}/image
GENIMAGE_CFG=${SDK_DIR}/mk_image/genimage-sdcard.cfg

TOOL_DIR=${PWD}
#HOST_DIR=${SDK_DIR}/output/my24ok_f1c200s_defconfig/host

do_install_tools() {
	echo -e "\033[1;33m  安装 mkimage image \033[0m"
	sudo apt install u-boot-tools genimage mtools  
}

do_make_image_dir () {
	echo -e ${SDK_DIR}
	echo -e "${IMAGE_DIR}"
	echo -e "\033[1;33m  创建文件夹 image \033[0m"
 
    if [ ! -d "${IMAGE_DIR}" ] ;then 
	   mkdir -p ${IMAGE_DIR}
	fi
    echo -e "\033[1;33m  请手动复制 u-boot-sunxi-with-spl.bin  zImage  devicetree.dtb rootfs.tar 到  ${IMAGE_DIR} \033[0m"
}



do_is_file_ok () {

    cd ${IMAGE_DIR}
	echo -e "\033[1;33m开始检查文件是否存在\033[0m"

	echo -e "\033[1;33m检查u-boot-sunxi-with-spl.bin\033[0m" 
	if [ ! -f "u-boot-sunxi-with-spl.bin" ] ;then 
	echo -e "\033[1;31m未找到 u-boot-sunxi-with-spl.bin \033[0m"
	#exit
	else
	echo -e "\033[1;33mu-boot-sunxi-with-spl.bin 存在 \033[0m"
	fi

	#	if [ ! -f "u-boot-sunxi-with-spl.bin" ] ;then 
	#echo -e "\033[1;31m未找到 u-boot-sunxi-with-spl.bin \033[0m"
	##exit
	#else
	#echo -e "\033[1;33mu-boot-sunxi-with-spl.bin 存在 \033[0m"
	#fi

}

do_copy_files () {
 echo -e "\033[1;33m开始 制作 image \033[0m"

STARTDIR=`pwd`
 
MKIMAGE="/usr/bin/mkimage"
IMAGE_ITS="kernel.its"
OUTPUT_NAME="kernel.itb"
 

cp -f ${SDK_DIR_BUILDROOT}/board/allwinner/generic/kernel.its "${IMAGE_DIR}/"
cd "${IMAGE_DIR}"
"${MKIMAGE}" -f ${IMAGE_ITS} ${OUTPUT_NAME}
 
cp -f ${SDK_DIR_BUILDROOT}/board/allwinner/generic/splash.bmp "${IMAGE_DIR}/" 
cp -f ${SDK_DIR_BUILDROOT}/board/allwinner/generic/genimage-sdcard.cfg "${IMAGE_DIR}/"
 
}

do_mk_image() {

echo -e "\033[1;33m mk image1 \033[0m"
cd ${TOOL_DIR}
./real.sh 
rm -rf tmp
echo -e "\033[1;33m mk image \033[0m"
/usr/bin/genimage --inputpath image --rootpath image --config genimage-sdcard.cfg
echo -e "\033[1;33m done \033[0m" 
}


set -e 
do_install_tools
do_make_image_dir
do_is_file_ok 
do_copy_files
do_mk_image